package com.smarthealthring.mm.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.jakewharton.rxbinding2.view.RxView;
import com.smarthealthring.mm.R;
import com.smarthealthring.mm.base.BaseActivity;
import com.smarthealthring.mm.base.IPresenter;
import com.smarthealthring.mm.utils.ComToolBar;
import com.smarthealthring.mm.utils.SPUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 消息设置
 */
public class MsgSetActivity extends BaseActivity {

    private OptionsPickerView notDisturbPicker;

    @BindView(R.id.ll_not_disturb)
    LinearLayout mLlNotDisturb;
    @BindView(R.id.tv_not_disturb)
    TextView mTvNotDisturb;
    @BindView(R.id.sw_get_msg)
    Switch mSwGetMsg;
    @BindView(R.id.sw_not_disturb)
    Switch mNotDisturb;

    @Override
    protected void initComToolBar() {
        ImageView ivLeft = ComToolBar.setleftImg(mContext, R.drawable.back_white);
        RxView.clicks(ivLeft)
                .subscribe(o -> finish());
        ComToolBar.setTitle(mContext, "消息设置");
    }

    @Override
    protected int getLayout() {
        return R.layout.act_msg_set;
    }

    @Override
    protected void initEventAndData(@Nullable Bundle savedInstanceState) {

        initNotDisturbPicker();
        mLlNotDisturb.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                notDisturbPicker.show();
            }
        });
        mSwGetMsg.setChecked(SPUtils.getBoolean(SPUtils.IS_GET_MSG, false));
        mNotDisturb.setChecked(SPUtils.getBoolean(SPUtils.IS_NOT_DISTURBB, false));
        mSwGetMsg.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    SPUtils.setBoolean(SPUtils.IS_GET_MSG, true);
                } else {
                    SPUtils.setBoolean(SPUtils.IS_GET_MSG, false);
                }
            }
        });
        mNotDisturb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    SPUtils.setBoolean(SPUtils.IS_NOT_DISTURBB, true);
                } else {
                    SPUtils.setBoolean(SPUtils.IS_NOT_DISTURBB, false);
                }
            }
        });

    }

    @Override
    protected IPresenter createPresenter() {
        return null;
    }


    /**
     * 选择器
     */
    private void initNotDisturbPicker() {
        String nowDisturbTime = SPUtils.getString(SPUtils.NOT_DISTURBB_TIME, "22:00~6:00");
        mTvNotDisturb.setText(nowDisturbTime);

        List<String> startList = new ArrayList();
        for (int i = 0; i < 24; i++) {
            startList.add(i + ":00");
        }
        List<String> endList = new ArrayList();
        for (int i = 0; i < 24; i++) {
            endList.add(i + ":00");
        }
        notDisturbPicker = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {

            @Override
            public void onOptionsSelect(int options1, int options2, int options3, View v) {
                String strStart = startList.get(options1);
                String strEnd = endList.get(options2);
                mTvNotDisturb.setText(strStart + "~" + strEnd);
                SPUtils.setString(SPUtils.NOT_DISTURBB_TIME, strStart + "~" + strEnd);
            }
        })
                .build();
        notDisturbPicker.setNPicker(startList, endList, null);
    }
}
